package com.kaiwukj.android.ufamily.mvp.ui.page.service.gm;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerServiceComponent;
import com.kaiwukj.android.ufamily.di.module.ServiceModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.GMAppointmentOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.GMOrderDetailResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.GMOrderResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.OrderGMWorkingListUrl)
public class GMOrderActivity extends BaseMvpActivity<GMOrderPresenter> implements ServiceContract.GMOrderView {

    @BindView(R.id.root_view)
    View rootView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private GMOrderAdapter mOrderAdapter;
    private GMAppointmentOrderAdapter mAppointmentOrderAdapter;

    private int page = 1;
    private int pageSize = 12;
    private boolean isLoadingMore = false;

    @Autowired
    int type = 0;//0:预约订单 1:装修订单


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerServiceComponent.builder()
                .serviceModule(new ServiceModule(this))
                .appComponent(appComponent)
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_list_normal;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {


        rootView.setBackgroundColor(ContextCompat.getColor(this, R.color.white_color));
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(15));
        if (type == 0) {
            tvTitle.setText("预约订单");
            mAppointmentOrderAdapter = new GMAppointmentOrderAdapter();
            rvList.setAdapter(mAppointmentOrderAdapter);
           /* mAppointmentOrderAdapter.setOnItemClickListener((adapter, view, position) -> {
                ARouter.getInstance().build(ARouterUrlKt.OrderGMDetailUrl)
                        .withString("title", mAppointmentOrderAdapter.getItem(position).getAppointmentTypeStr())
                        .withInt("orderId", mAppointmentOrderAdapter.getItem(position).getId())
                        .navigation();
            });*/
        } else {
            tvTitle.setText("装修订单");
            mOrderAdapter = new GMOrderAdapter();
            rvList.setAdapter(mOrderAdapter);
            mOrderAdapter.setOnItemClickListener((adapter, view, position) -> {
                ARouter.getInstance().build(ARouterUrlKt.OrderGMDetailUrl)
                        .withString("title", mOrderAdapter.getItem(position).getProjectName())
                        .withInt("orderId", mOrderAdapter.getItem(position).getId())
                        .navigation();
            });
        }

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadingMore = true;
                loadData(++page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData(page = 1);
            }
        });
    }

    private void loadData(int page) {
        if (type == 0) {
            mPresenter.fetchGMAppointmentOrder(page, pageSize);
        } else {
            mPresenter.fetchGMWorkingOrder(page, pageSize);
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        emptyView.show(true);
        loadData(page);
    }

    @Override
    public void onFetchGMOrder(List<GMOrderResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            mOrderAdapter.addData(results);
            if (results.size() < pageSize) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            mOrderAdapter.setList(results);
            if (results.size() < pageSize) {
                refreshLayout.finishRefreshWithNoMoreData();
            } else {
                refreshLayout.finishRefresh(true);
            }
        }
        if (mOrderAdapter.getItemCount() == 0) {
            emptyView.show("暂没有订单", "");
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            refreshLayout.finishLoadMore(false);
        } else {
            refreshLayout.finishRefresh(false);
        }
    }

    @Override
    public void onFetchGMAppointmentOrder(List<GMAppointmentOrderResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            mAppointmentOrderAdapter.addData(results);
            if (results.size() < pageSize) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            mAppointmentOrderAdapter.setList(results);
            if (results.size() < pageSize) {
                refreshLayout.finishRefreshWithNoMoreData();
            } else {
                refreshLayout.finishRefresh(true);
            }
        }
        if (mAppointmentOrderAdapter.getItemCount() == 0) {
            emptyView.show("暂没有订单", "");
        }
    }

    @Override
    public void onGetOrderDetail(GMOrderDetailResult result) {

    }
}
